home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / svd.cc < prev    next >
C/C++ Source or Header  |  1996-11-03  |  3KB  |  143 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include "CmplxSVD.h"
  28. #include "dbleSVD.h"
  29.  
  30. #include "defun-dld.h"
  31. #include "error.h"
  32. #include "gripes.h"
  33. #include "help.h"
  34. #include "mappers.h"
  35. #include "oct-obj.h"
  36. #include "pr-output.h"
  37. #include "utils.h"
  38.  
  39. DEFUN_DLD (svd, args, nargout,
  40.   "S = svd (X) or [U, S, V] = svd (X [, 0])\n\
  41. \n\
  42. Compute the singular value decomposition of X.  Given a second input\n\
  43. argument, an `economy' sized factorization is computed that omits\n\
  44. unnecessary rows and columns of U and V.\n\
  45. \n\
  46. X may not contain any Inf or NaN values.")
  47. {
  48.   octave_value_list retval;
  49.  
  50.   int nargin = args.length ();
  51.  
  52.   if (nargin < 1 || nargin > 2 || nargout == 2 || nargout > 3)
  53.     {
  54.       print_usage ("svd");
  55.       return retval;
  56.     }
  57.  
  58.   octave_value arg = args(0);
  59.  
  60.   int arg_is_empty = empty_arg ("svd", arg.rows (), arg.columns ());
  61.  
  62.   if (arg_is_empty < 0)
  63.     return retval;
  64.   else if (arg_is_empty > 0)
  65.     return octave_value_list (3, Matrix ());
  66.  
  67.   SVD::type type = ((nargout == 0 || nargout == 1)
  68.             ? SVD::sigma_only
  69.             : (nargin == 2) ? SVD::economy : SVD::std);
  70.  
  71.   if (arg.is_real_type ())
  72.     {
  73.       Matrix tmp = arg.matrix_value ();
  74.  
  75.       if (! error_state)
  76.     {
  77.       if (tmp.any_element_is_inf_or_nan ())
  78.         {
  79.           error ("svd: cannot take SVD of matrix containing Inf or\
  80.  NaN values"); 
  81.           return retval;
  82.         }
  83.  
  84.       SVD result (tmp, type);
  85.  
  86.       DiagMatrix sigma = result.singular_values ();
  87.  
  88.       if (nargout == 0 || nargout == 1)
  89.         {
  90.           retval(0) = octave_value (sigma.diag (), 1);
  91.         }
  92.       else
  93.         {
  94.           retval(2) = result.right_singular_matrix ();
  95.           retval(1) = sigma;
  96.           retval(0) = result.left_singular_matrix ();
  97.         }
  98.     }
  99.     }
  100.   else if (arg.is_complex_type ())
  101.     {
  102.       ComplexMatrix ctmp = arg.complex_matrix_value ();
  103.  
  104.       if (! error_state)
  105.     {
  106.       if (ctmp.any_element_is_inf_or_nan ())
  107.         {
  108.           error ("svd: cannot take SVD of matrix containing Inf or\
  109.  NaN values"); 
  110.           return retval;
  111.         }
  112.  
  113.       ComplexSVD result (ctmp, type);
  114.  
  115.       DiagMatrix sigma = result.singular_values ();
  116.  
  117.       if (nargout == 0 || nargout == 1)
  118.         {
  119.           retval(0) = octave_value (sigma.diag (), 1);
  120.         }
  121.       else
  122.         {
  123.           retval(2) = result.right_singular_matrix ();
  124.           retval(1) = sigma;
  125.           retval(0) = result.left_singular_matrix ();
  126.         }
  127.     }
  128.     }
  129.   else
  130.     {
  131.       gripe_wrong_type_arg ("svd", arg);
  132.       return retval;
  133.     }
  134.  
  135.   return retval;
  136. }
  137.  
  138. /*
  139. ;;; Local Variables: ***
  140. ;;; mode: C++ ***
  141. ;;; End: ***
  142. */
  143.